<template>
    <div class="videoCateGory">
        <el-row class="title">
            <h3>视频分类</h3>
        </el-row>
        <el-row>
            <el-table :data="videoCategoryList" border>
                <el-table-column prop="videoCategoryId" label="id" align="center"></el-table-column>
                <el-table-column prop="videoCategoryName" label="分类名" align="center"></el-table-column>
                <el-table-column prop="orderNumber" label="排序值" align="center"></el-table-column>
                <el-table-column label="封面" align="center">
                    <template slot-scope="scope"><img :src="getImageUrl(scope.row.videoCategoryImageUrl)" alt="" class="poster"></template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="200px">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" @click="updateDatas(scope.$index, scope.row)">修改</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-dialog title="修改视频分类" :visible.sync="dialogShow" width="400px" @close="closeDialog">
            <el-form label-width="120px">
                <el-form-item label="分类名">
                    <el-input v-model="videoCategoryName" placeholder="分类名"></el-input>
                </el-form-item>
                <el-form-item label="排序值">
                    <el-input-number v-model="orderNumber" :min="1" label="排序值"></el-input-number>
                </el-form-item>
                <el-form-item label="视频封面">
                    <el-upload :headers="uploadHeader" :action="BASEURL + '/upload/single/poster'" :show-file-list="false" :on-success="uploadPosterSuccess">
                        <el-button size="small" type="primary">上传封面</el-button>
                    </el-upload>
                    <div v-if="poster">
                        <img :src="UPLOADURL + poster" alt="" class="poster-thumb">
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="save" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="count">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { getVideoCategoryLists, updateVideoCategoryById } from '@/api/api'
export default {
    data: function() {
        return {
            videoCategoryList: [],
            dialogShow: false,
            loading: false,
            videoCategoryName: '',
            poster: '',
            orderNumber: 1,
            index: 0,
            videoCategoryId: null,
            pageId: 1,
            pageSize: 10,
            count: 0,
            uploadHeader: {
                TOKEN: sessionStorage.getItem('jwt')
            }
        }
    },
    created: function() {
        this.getData();
    },
    methods: {
        getData: function() {
            getVideoCategoryLists().then(res => {
                console.log(res)
                this.videoCategoryList = res.list;
                this.count = res.count;
            })
        },
        closeDialog: function() {
            this.dialogShow = false;
            this.videoCategoryName = '';
            this.videoCategoryId = null;
            this.orderNumber = 1;
            this.poster = '';
        },
        save: function() {
            updateVideoCategoryById(this.videoCategoryId, {
                orderNumber: this.orderNumber,
                videoCategoryName: this.videoCategoryName,
                videoCategoryImageUrl: this.poster
            }).then(res => {
                this.getData();
                this.$message({
                    message: '修改成功!',
                    duration: 2000,
                    type: 'success'
                });
                this.closeDialog();
            })
        },
        updateDatas: function(index, row) {
            this.videoCategoryName = row.videoCategoryName;
            this.orderNumber = row.orderNumber;
            this.poster = row.videoCategoryImageUrl;
            this.videoCategoryId = row.videoCategoryId;
            this.index = index;
            this.dialogShow = true;
        },
        uploadPosterSuccess: function(res) {
            if (res.status) {
                this.$message({
                    message: '上传成功',
                    type: 'success'
                })
                this.poster = res.data.webPath;
            } else {
                this.$message({
                    message: '上传失败',
                    type: 'error'
                })
            }
        },
        currentChange: function(val) {
            this.$router.push('?page=' + val);
            this.pageId = val;
            this.getData();
        }
    }
}
</script>
<style scoped lang="scss">
.title {
    font-size: 12px;
    margin-bottom: 5px;
    h3 {
        display: inline-block;
        padding: 5px 0;
        margin: 0;
        font-weight: normal;
        color: #000;
        border-bottom: 2px solid #000;
    }
}
.poster{
    width: 200px;
}
.poster-thumb{
    max-width: 100%;
}
</style>